<?php

/**
 * terminal actions.
 *
 * @package    TB
 * @subpackage terminal
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class terminalActions extends sfActions {
    public function executeIndex(sfWebRequest $request) {
	$this->lastVersion = TuPeer::lastVersion();
	$this->tb_tu_list = TuPeer::doSelect(new Criteria());
	$this->enquetes = EnquetePeer::doSelect(new Criteria());
    }

    public function executeShow(sfWebRequest $request) {
	$this->tb_tu = TuPeer::retrieveByPk($request->getParameter('id'));
	$this->forward404Unless($this->tb_tu);

	$this->nbP = $this->tb_tu->getNbPatients();

	$this->tabCadenas = TuPeer::getIconMaj(($this->tb_tu->getEtatMaj(TuPeer::lastVersion())));

	$criteria = new Criteria();
	$criteria->add(EnquetePeer::TU_ID, $request->getParameter("id"));
	$this->enquetes = EnquetePeer::doSelect($criteria);
    }

    public function executeEdit(sfWebRequest $request) {
	$this->forward404Unless(
	    $tb_tu = TuPeer::retrieveByPk($request->getParameter('id')),
	    sprintf('Object tb_tu does not exist (%s).', $request->getParameter('id'))
	);
	$locked = $tb_tu->getIsMajLocked();

	$tb_tu->setIsMajLocked(!$locked);

	$tb_tu->setIsMajAsked(false);
	$tb_tu->setVersionDemandee(null);
	$tb_tu->save();
	$this->redirect('terminal/show?id='.$request->getParameter('id'));
    }

    public function executeNew(sfWebRequest $request) {
	$this->form = new TuForm();
	$this->form->setType("add");
    }

    public function executeCreate(sfWebRequest $request) {
	$this->forward404Unless($request->isMethod('post'));
	$this->form = new TuForm();
	$this->form->setType("add");
	if ($this->processForm($request, $this->form)) $this->setTemplate('creation');
	else $this->setTemplate('new');
    }

    public function executeCreation(sfWebRequest $request) {
	$this->forward404Unless($this->tu = TuPeer::retrieveByPK($request->getParameter('id')));
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
	$form->bind($request->getParameter($form->getName()));
	if ($form->isValid()) {
	    $tu = $form->save();
	    $tu->setCle(sha1(microtime(NULL)*100000));
	    $tu->save();
	    $this->tu = $tu;
	    return true;
	}
	return false;
    }
}
